package com.yunhe.device.controller.device;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.device.domain.device.EquipStatusCountVo;
import com.yunhe.device.domain.device.EquipTypeCountVo;
import com.yunhe.device.domain.device.Device;
import com.yunhe.device.model.device.*;
import com.yunhe.device.service.device.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName EquipmentController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/15 17:36
 * @Version 1.0
 **/
@Api(value = "Equipment", description = "设备维护", tags = {"Base_Equipment"})
@RestController
@RequestMapping("/equips")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    /**
     * 新增设备
     * @param equipmentBo 设备对象
     */
    @ApiOperation("新增设备")
    @PostMapping(value = "/createEquipment")
    public ResultObject<Device> createEquipment(@RequestBody EquipmentBo equipmentBo) throws UniqueConstraintsException, PinyinException {
        Device newEquipment = equipmentService.createEquipmente(equipmentBo);
        return new ResultObject(newEquipment);
    }

    /**
     * 查询设备列表
     * @param equipmentQueryCondition 设备对象
     */
    @ApiOperation("查询设备列表")
    @PostMapping(value = "/getEquipByQueryCondition")
    public ResultObject<List<EquipmentVo>> getEquipByQueryCondition(@RequestBody EquipmentQueryCondition equipmentQueryCondition) {
        Iterable<EquipmentVo> equipmentVos = equipmentService.getEquipByQueryCondition(equipmentQueryCondition);
        return new ResultObject(equipmentVos);
    }

    /**
     * 查询设备以及设备属性详情
     * @param id
     */
    @ApiOperation("查询设备以及设备属性详情")
    @GetMapping (value = "/getEquipById")
    public ResultObject<EquipmentBo> getEquipById(@ApiParam(value = "id",required = true)
                                                      @RequestParam(value = "id",required = true) Long id) throws ObjectNotFoundException {
        EquipmentBo equipmentBo = equipmentService.getEquipById(id);
        return new ResultObject(equipmentBo);
    }

    @ApiOperation("查询设备以及点号类型详情")
    @PostMapping (value = "/getEquipAndAnlogTypeById")
    public ResultObject<DeviceAnlogTypeVo> getEquipAndAnlogTypeById(@RequestBody EquipAnlogTypeQueryCondition equipAnlogTypeQueryCondition) throws ObjectNotFoundException {
        DeviceAnlogTypeVo deviceAnlogTypeVo = equipmentService.getEquipAndAnlogTypeById(equipAnlogTypeQueryCondition);
        return new ResultObject(deviceAnlogTypeVo);
    }

    @ApiOperation("编辑设备以及设备属性详情")
    @PostMapping (value = "/updateEquipById")
    public ResultObject<Void> updateEquipById(@RequestBody  EquipmentBo equipmentBo) throws ObjectNotFoundException, UniqueConstraintsException, PinyinException {
        equipmentService.updateEquipById(equipmentBo);
        return new ResultObject();
    }

    @ApiOperation("编辑设备以及点号类型详情")
    @PostMapping (value = "/updateEquipAndAnlogTypById")
    public ResultObject<Void> updateEquipAndAnlogTypById(@RequestBody  EquipmentBo equipmentBo) throws ObjectNotFoundException, UniqueConstraintsException, ArgumentErrorException, PinyinException {
        equipmentService.updateEquipAndAnlogTypById(equipmentBo);
        return new ResultObject();
    }

    @ApiOperation("根据id删除设备")
    @DeleteMapping (value = "/deleteEquipById")
    public ResultObject<Void> deleteEquipById(@ApiParam(value = "id",required = true)
                                                  @RequestParam(value = "id",required = true) Long id) throws ObjectNotFoundException, UniqueConstraintsException {
        equipmentService.deleteEquipById(id);
        return new ResultObject();
    }

    @ApiOperation("查询设备状态统计")
    @GetMapping (value = "/getEquipStatusCountByOwnedPark")
    public ResultObject<EquipStatusCountVo> getEquipStatusCountByOwnedPark(@ApiParam(value = "园区id",required = false)
                                                           @RequestParam(value = "realStationIds",required = false) List<Long> realStationIds) throws ObjectNotFoundException, UniqueConstraintsException {
        EquipStatusCountVo equipStatusCountVo = equipmentService.getEquipCountByOwnedPark(realStationIds);
        return new ResultObject(equipStatusCountVo);
    }

    @ApiOperation("查询设备设备类型统计")
    @GetMapping (value = "/getEquipTypeCountByOwnedPark")
    public ResultObject<List<EquipTypeCountVo>> getEquipTypeCountByOwnedPark(@ApiParam(value = "园区id",required = false)
                                                                     @RequestParam(value = "realStationIds",required = false) List<Long> realStationIds) throws ObjectNotFoundException, UniqueConstraintsException {
        List<EquipTypeCountVo> equipStatusCountVos = equipmentService.getEquipTypeCountByOwnedPark(realStationIds);
        return new ResultObject(equipStatusCountVos);
    }

    @ApiOperation("通过厂房查询设备")
    @GetMapping(value = "/getEquipByOfficeId")
    public ResultObject<List<Device>> getEquipByOfficeId(@ApiParam(value = "厂房id", required = true)
                                                             @RequestParam(value = "officeIds", required = true) List<Long> officeIds){
        List<Device> equipByOfficeId = equipmentService.getEquipByOfficeId(officeIds);
        return new ResultObject(equipByOfficeId);
    }

    @ApiOperation("房源管理查看设备信息")
    @GetMapping(value = "/getEquipInfoByOfficeId")
    public ResultObject<Page<OfficeManageDeviceVO>> getEquipInfoByOfficeId(@RequestParam(value = "officeId", required = true) Long officeId,
                                                                           @ModelAttribute PageParam pageParam){
        Page<OfficeManageDeviceVO> equipByOfficeId = equipmentService.getEquipInfoByOfficeId(officeId, pageParam);
        return new ResultObject(equipByOfficeId);
    }

    @ApiOperation("设备批量导入模板下载")
    @GetMapping(value = "/downDeviceTemplate")
    public ResponseEntity<ResultObject> downDeviceTemplate(HttpServletResponse httpServletResponse,
                                                           @RequestParam(value = "equipTypeId", required = true) Long equipTypeId) throws ArgumentErrorException, IOException, InvalidFormatException {
        equipmentService.downDeviceTemplate(equipTypeId,httpServletResponse);
        return null;
    }

    @ApiOperation("设备批量导入")
    @PostMapping(value = "/importDevice")
    public ResponseEntity<ResultObject> importDevice(@RequestBody MultipartFile file) throws ArgumentErrorException, IOException, InvalidFormatException, UniqueConstraintsException, PinyinException, ObjectNotFoundException {
        equipmentService.importDevice(file);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

}
